home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / MacApp Documentation / MacApp AppleLink Messages / MacApp.Tech$ Apr 89 / T0010-Re Dangerous Lock i-Apr89 < prev    next >
Encoding:
Text File  |  1989-04-12  |  4.5 KB  |  157 lines  |  [TEXT/GEOL]

  1. Item    4250386                         12-April-89        09:38
  2.  
  3. From:   UK0004                          Bacchus & Smith, Paul Smith
  4.  
  5. To:     MACAPP.TECH$                    MACAPP Tech
  6.         MACDTS                          Macintosh Developer Technical Supt.
  7.  
  8. Sub:    Re Dangerous Lock in 2.0b7
  9.  
  10.  
  11. DISCLAIMER: Let me start by saying that we do not have MacApp 2.0b7: we are
  12. still using MacApp 2.0b5, and my comments a solely as a result of seeing the
  13. link from Hermes Systems SA (Guy Fiems and Philippe Lange) this afternoon.
  14.  
  15. I understand, from Hermes' link, that MacApp 2.0b7 implements "TObject.Lock"
  16. and ".Unlock" methods. This is BRILLIANT! We have used ".Lock" and ".Unlock"
  17. overrides ourselves since early betas of MacApp 1.0.
  18.  
  19. Like Hermes, I think the MacApp 2.0b7 implementation is dangerous. I would go
  20. further, and say that, in my opinion, it positively invites disaster. BUT, I
  21. _absolutely_ disagree with Hermes' suggested solution. There is a better way.
  22.  
  23. The better way, as described below, allows ANY NUMBER (up to maxint) of nested
  24. locks and unlocks, as long as they balance out in the end.
  25.  
  26. In my view, the best way to implement ".Lock" and ".Unlock" is by keeping a
  27. Lock count (somewhat like traditional Semaphores, as used for synchronising
  28. sequential processes, access to shared resources, etc, etc).
  29.  
  30. The basic principle is this: the lock counter variable keeps a record of how
  31. many times the object has been locked; it starts off at zero, and is
  32. incremented whenever the object is locked, and decremented whenever the object
  33. is unlocked; the object is only actually locked or unlocked when the lock count
  34. changes from zero to one or vice versa.
  35.  
  36. Here's another explanation (more like pseudocode):
  37.  
  38.     • an object field is declared, called fLockCount, initialised to 0
  39.  
  40.     • to lock something, you call a routine that does this:
  41.  
  42.         if fLockCount equals 0, then LOCK the object;
  43.         add 1 to fLockCount;
  44.  
  45.     • to unlock the object, you call a routine that does this:
  46.  
  47.         subtract 1 from fLockCount;
  48.         if fLockCount equals 0, then UNLOCK the object;
  49.  
  50. As I said above, we have been using this technique successfully for some time.
  51. I have pulled the relevant object declarations and code from our re-usable
  52. source code library and included it below.
  53.  
  54. Maybe this sounds rather pig headed, but: If MacApp 2.0 final does not
  55. implement something like the code below, I will certainly NOT let my
  56. development engineers use the standard methods - we shall continue to override
  57. TObject with our own TLObject overrides.
  58.  
  59. Of course, if (I say, hopefully) the gentlemen at Hermes are mistaken about
  60. what 2.0b7 actually does in this respect, then all's well and good
  61.  
  62. Best regards,
  63.  
  64. Paul G Smith
  65. ============
  66.  
  67.  
  68.  
  69. { sample code follows }
  70.  
  71. { ---------------------------------------------------------------------- }
  72. { declaration of TLObject class (from INTERFACE) }
  73.  
  74.     TLObject = OBJECT (TObject)                     { Lockable object }
  75.  
  76.         fLockCount    : Integer;                { Lock count, incremented when the
  77.                                                                     object is locked. }
  78.  
  79.         Procedure TLObject.ILObject;                { Initialise object (Set fLockcount to
  80. 0) }
  81.  
  82.         Procedure TLObject.Free; OVERRIDE;  { Free object }
  83.  
  84.         Procedure TLObject.Lock;                    { Lock object }
  85.  
  86.         Procedure TLObject.UnLock;          { Unlock object }
  87.  
  88. {$IFC qDebug}
  89.         Procedure TLObject.Inspect; OVERRIDE;   { Writes out lock count }
  90. {$ENDC}
  91.  
  92.     end;                                    { TLObject }
  93.  
  94.  
  95. { TLObject methods ------------------------------------------------------ }
  96. { from IMPLEMENTATION }
  97.  
  98. {$S ARes}
  99.  
  100. Procedure TLObject.ILObject;
  101.         { Initialise object (Set fLockcount to 0) }
  102. Begin
  103.         fLockCount := 0;
  104. End;
  105.  
  106.  
  107. Procedure TLObject.Free; OVERRIDE;
  108.         { Free object }
  109. Begin
  110.     {$IFC qDebug}
  111.         If fLockCount <> 0 then
  112.                 ProgramBreak ('TLObject.Free : Lock count is not 0');
  113.     {$ENDC}
  114.         INHERITED Free;
  115. End;
  116.  
  117.  
  118. Procedure TLObject.Lock;
  119. { Lock object }
  120. Begin
  121.         if fLockCount = 0 then
  122.                 begin
  123.                 HLock (Handle (SELF));
  124.                 end;
  125.         fLockCount := fLockCount + 1;
  126. End;
  127.  
  128.  
  129. Procedure TLObject.UnLock;
  130. { Unlock object }
  131. Begin
  132.         {$IFC qDebug}
  133.         If fLockCount = 0 then
  134.             begin
  135.                 ProgramBreak ('TLObject.UnLock : Lock count equals 0');
  136.             end
  137.         else
  138. {$ENDC}
  139.                 fLockCount := fLockCount - 1;
  140.         if fLockCount = 0 then
  141.                 HUnLock (Handle (SELF));
  142. End;
  143.  
  144.  
  145. {$IFC qDebug}
  146. Procedure TLObject.Inspect; OVERRIDE;
  147. Begin
  148.     INHERITED Inspect;
  149.         Writeln('  fLockCount = ', fLockCount:1);
  150. End;
  151. {$ENDC}
  152.  
  153. { ---------------------------------------------------------------------- }
  154.  
  155.  
  156.  
  157.